
import pygame,os.path,random,time

import ball
import player
from outman_global import *

def load_image(file,rect):
        '''loads an image'''
        file = os.path.join('pic',file)
        try:
                surface = pygame.image.load(file)
        except pygame.error:
                raise SystemExit, 'Could not load image "%s" %s' %(file,pygame.get_error())
        surface.set_colorkey( surface.get_at([0,0]) )
        return pygame.transform.scale(surface,rect).convert()      

def createLeftBall( count ):
        for i in range(count):
                y_pos = random.randrange(0,win_size[1])
                ball.CBall(0,y_pos,player_init_pos_x,player_init_pos_y,ball_speed)

def createTopBall( count ):
        for i in range(count):
                x_pos = random.randrange(0,win_size[0])
                ball.CBall(x_pos,0,player_init_pos_x,player_init_pos_y,ball_speed)
                
def createRightBall( count ):
        for i in range(count):
                y_pos = random.randrange(0,win_size[1])
                ball.CBall(win_size[0]-ball_rect[0],y_pos,player_init_pos_x,player_init_pos_y,ball_speed)

def createDownBall( count ):
        for i in range(count):
                x_pos = random.randrange(0,win_size[0])
                ball.CBall(x_pos,win_size[1]-ball_rect[1],player_init_pos_x,player_init_pos_y,ball_speed)

def startGame():
        pass

def gameOver( screen,elapsedTime ):
        global game_status
        game_status = GAMEOVER
        font = pygame.font.Font(None,40)
        msg = "Alived for %d seconds" %elapsedTime
        infoSurface = font.render(msg, 0, [200,200,200])
        screen.blit( infoSurface, (0,0) )

def restartGame( screen ):
        global game_status,startTime
        global outman
        game_status = START
        startTime = time.time()
        outman = player.CPlayer()
        screen.fill([0,0,0])

def stopGame():
        pass

def resumeGame():
        pass

        

pygame.init()

screen = pygame.display.set_mode(win_size)
pygame.display.set_caption(" Are you Outman? ")

image = load_image('ball.png',ball_rect)
ball.CBall.images = [image]
image = load_image('outman.gif',player_rect)
player.CPlayer.images = [image]


#Initialize Game Groups
balls = pygame.sprite.Group()
players = pygame.sprite.Group()
all = pygame.sprite.RenderUpdates()

ball.CBall.containers = balls,all
player.CPlayer.containers = players,all

outman = player.CPlayer()
clock = pygame.time.Clock()

# Loop until user close window
done = False
game_status = START
min_num = 5
max_num = 10
ball_appear_proability = 0.05
startTime = time.time()

while done == False:

        if game_status == START:
                if len(balls) < max_ball_num and random.random() < ball_appear_proability:
                        createLeftBall( random.randrange(min_num,max_num) )
                        
                if len(balls) < max_ball_num and random.random() < ball_appear_proability:
                        createTopBall( random.randrange(min_num,max_num) )
                        
                if len(balls) < max_ball_num and random.random() < ball_appear_proability:
                        createRightBall( random.randrange(min_num,max_num) )                                
                if len(balls) < max_ball_num and random.random() < ball_appear_proability:
                        createDownBall( random.randrange(min_num,max_num) )
                                
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True

                if game_status == START:                
                        if event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_LEFT:
                                        outman.set_xspeed(-3)
                                if event.key == pygame.K_RIGHT:
                                        outman.set_xspeed(3)
                                if event.key == pygame.K_UP:
                                        outman.set_yspeed(-3)
                                if event.key == pygame.K_DOWN:
                                        outman.set_yspeed(3)
                                if event.key in [pygame.K_SPACE]:
                                        stopGame()
                                        
                        if event.type == pygame.KEYUP:
                                if event.key == pygame.K_LEFT:
                                        outman.set_xspeed(0)
                                if event.key == pygame.K_RIGHT:
                                        outman.set_xspeed(0)
                                if event.key == pygame.K_UP:
                                        outman.set_yspeed(0)
                                if event.key == pygame.K_DOWN:
                                        outman.set_yspeed(0)
        
                if game_status == STOP:
                        if event.type == pygame.KEYDOWN:
                                if event.key in [pygame.K_SPACE]:
                                        resumeGame()

                if game_status == GAMEOVER:
                        if event.type == pygame.KEYDOWN:
                                if event.key in [pygame.K_SPACE]:
                                        restartGame(screen) 
        

        if game_status == START:
                
                all.update()
                all.clear(screen,pygame.Surface(win_size))
                dirty = all.draw(screen)
                pygame.display.update(dirty)
                pygame.display.flip()
        
                for item in pygame.sprite.spritecollide(outman,balls,1):
                        all.empty()
                        balls.empty()
                        players.empty()
                        all.clear(screen,pygame.Surface(win_size))
                        elapsedTime = time.time()- startTime
                        gameOver(screen,elapsedTime)
                        pygame.display.flip()
        clock.tick(20)
    

pygame.quit()
